//签到积分云函数
// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init()

const db = cloud.database()

const _ = db.command

// 云函数入口函数
exports.main = async(event, context) => {

  let _openid = event.openid
  let number = event.continueDays
  let sense = event.sense
  const signIntegral = await db.collection('user').where({
    _id: _openid
  }).update({
    data: {
      totalPoints: _.inc(number)
    }
  })
  const integral = await db.collection('integral').add({
    data: {
      _openid,
      sense, //1:加积分，0：减积分
      totalPoints: number,
      createTime: db.serverDate(), // 服务端的时间
    }
  })

  return {
    signIntegral,
    integral
  }

}